<?php
class Search_Form_RegularForm extends Ac_Form
{
    private $settings;

    public function __construct($options = array())
    {
        $this->settings = !empty($options['settings']) ? $options['settings'] : array();
        parent::__construct($options);
    }

    public function init()
    {
        parent::init();

        $element = new Zend_Form_Element_Hidden('keyword');
        $element->setValue($_GET['keyword']);
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);

        $locations = array();
        $locs = Model_Location::all();
        foreach($locs as $k => $loc) {
            if (count($loc['path']) == 1) {
                $locations[(string) $k] = array('name' => $loc);
            } else {
                $parent = $loc['path'][0];
                $tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();

                $tmp['children'][$k]         = $loc;
                $locations[(string) $parent] = $tmp;
            }
        }

        $this->addElement('multiCheckboxSelect', 'district', array(
            'multiOptions' => $locations,
            'value'        => $this->settings['district']
        ));

        $this->addElement('jqslider', 'price', array(
                    'jQueryParams' => array(
                        'min'      => !empty($this->settings['price']['min']) ? $this->settings['price']['min'] : 0,
                        'max'      => !empty($this->settings['price']['max']) ? $this->settings['price']['max'] : 150000,
                        'values'   => !empty($this->settings['price']['start']) ? $this->settings['price']['start'] : array('0', '150000'),
                        'change'   => 'refineSearch()', 
                        'currency' => '$HKD',
                        'range'    => true,
                        'step'     => 500,
                        
                        ),
                    'label'       => 'Price <span id="price-label"></span> <span id="price-1-label"></span>',
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            array('Label', array('escape' => false)),
                            array(array('row' => 'HtmlTag'), array('tag' => 'p', 'class'=>'inputText')),
                    )
                )
        );


        /* Newsletter */
        $this->addElement('checkbox', 'has_image', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators' => $this->elementCheckBoxDecorators,
            'attribs'    => array('class' =>'inputCheckBox'),
            'required'   => false,
            'label'      => 'Has an image',
            'value'      => $this->settings['has_image'],
        ));

        $this->addElement('button', 'v', array(
            'required'    => false,
            'ignore'      => true,
            'type'        => 'submit',
            'label'       => 'Go!',
            'decorators'  => $this->elementButtonDecorators,
        ));
    }

    function populate(array $values)
    {
        if (isset($values['price']) && isset($values['price-1'])) {
            $this->settings['price']['start'] = array($values['price'], $values['price-1']);
        }

        $this->settings['district'] = '';
        if (isset($values['district'])) {
            $this->settings['district'] = $_GET['district'];
        }
        $this->settings['has_image'] = !empty($values['has_image']);
    }
}